package com.vrv.netmgr.persistent;

import android.content.Context;

import com.vrv.common.util.SharedPreferencesHelper;
import com.vrv.common.util.StringUtils;
import com.vrv.netmgr.vo.LoginInfo;

/**
 * @Description 登录信息本地保存帮助类
 * @author gaopei
 * @date
 */
public class LoginSharedPreferencesHelper {

	/**
	 * 登陆信息
	 */
	// 登录用户名Key
	public static final String LOGIN_INFO_KEY = "loginInfo";
	// 登录用户ID
	public static final String LOGIN_ID = "id";
	// 登录用户名Key
	public static final String LOGIN_NAME_KEY = "name";
	// 登录密码Key
	public static final String LOGIN_PASSWORD_KEY = "password";
	// 登录记住密码Key
	public static final String LOGIN_REMEMBER_KEY = "remember";
	// 自动登录Key
	public static final String LOGIN_AUTOLOGIN_KEY = "autoLoing";
	//设置全屏key
	public static final String LOGIN_FULLSCREEN_KEY = "fullScreen";

	/**
	 * 获取登录信息
	 *
	 * @param context
	 * @return
	 */
	public static LoginInfo getLoginInfo(Context context) {
		SharedPreferencesHelper spHelper = new SharedPreferencesHelper(context,
				LOGIN_INFO_KEY);
		String rememberPassword = spHelper.getValue(LOGIN_REMEMBER_KEY);
		String autoLogin = spHelper.getValue(LOGIN_AUTOLOGIN_KEY);
		String fullScreen = spHelper.getValue(LOGIN_FULLSCREEN_KEY);
		LoginInfo loginInfo = new LoginInfo();
		loginInfo.setRememberPasswd(Boolean.valueOf(rememberPassword));
		loginInfo.setAutoLogin(Boolean.valueOf(autoLogin));
		loginInfo.setFullScreen(Boolean.valueOf(fullScreen));
		loginInfo.setLoginName(spHelper.getValue(LOGIN_NAME_KEY));
		loginInfo.setLoginPasswd(spHelper.getValue(LOGIN_PASSWORD_KEY));
		loginInfo.setID(spHelper.getValue(LOGIN_ID));

		return loginInfo;
	}

	/**
	 * 保存登录信息
	 *
	 * @param context
	 * @return
	 */
	public static void saveLoginInfo(Context context, LoginInfo loginInfo) {
		SharedPreferencesHelper spHelper = new SharedPreferencesHelper(context,
				LOGIN_INFO_KEY);
		if (loginInfo != null) {
			if (loginInfo.isRememberPasswd())
				spHelper.putValue(LOGIN_REMEMBER_KEY, "true");
			else
				spHelper.putValue(LOGIN_REMEMBER_KEY, "false");
			if(loginInfo.isAutoLogin()){
				spHelper.putValue(LOGIN_AUTOLOGIN_KEY, "true");
			}else{
				spHelper.putValue(LOGIN_AUTOLOGIN_KEY, "false");
			}
			if(loginInfo.isFullScreen()){
				spHelper.putValue(LOGIN_FULLSCREEN_KEY, "true");
			}else{
				spHelper.putValue(LOGIN_FULLSCREEN_KEY, "false");
			}
			if (StringUtils.isNotBlank(loginInfo.getLoginName()))
				spHelper.putValue(LOGIN_NAME_KEY, loginInfo.getLoginName());
			if (StringUtils.isNotBlank(loginInfo.getLoginPasswd()))
				spHelper.putValue(LOGIN_PASSWORD_KEY,
						loginInfo.getLoginPasswd());
			if (StringUtils.isNotBlank(loginInfo.getID()))
				spHelper.putValue(LOGIN_ID, loginInfo.getID());
		} else {
			spHelper.putValue(LOGIN_REMEMBER_KEY, "false");
			spHelper.putValue(LOGIN_AUTOLOGIN_KEY, "false");
			spHelper.putValue(LOGIN_FULLSCREEN_KEY, "false");
			spHelper.putValue(LOGIN_NAME_KEY, "");
			spHelper.putValue(LOGIN_PASSWORD_KEY, "");
		}
	}

	/**
	 * 修改密码后保存信息
	 *
	 * @param context
	 * @param loginInfo
	 */
	public static void saveUpdatePwdInfo(Context context, String password) {
		SharedPreferencesHelper spHelper = new SharedPreferencesHelper(context,
				LOGIN_INFO_KEY);
		spHelper.putValue(LOGIN_PASSWORD_KEY, password);
	}
}
